#include "AnimationSequence.h"
#include <math.h>
#include "Constants.h"

AnimationSequence::AnimationSequence(Ogre::SceneManager * mScene) {
	_mScene = mScene;

	elapsedTime = 0;
	explosionCol = new ExplosionColumn(mScene);
	explosionMush = new ExplosionMush(mScene);
	shockwave = new ShockWave(mScene,"Examples/Dust",150);
	baseExplosion = new BaseExplosion(mScene);
	anim_events = std::vector<AnimationEvent*>();
	doubleShockWaves = std::vector<AnimationEvent*>();

	doubleShockWaves.push_back(new ShockWave(mScene,"TopShockwave", 1250,500));
	doubleShockWaves.push_back(new ShockWave(mScene,"TopShockwave", 2000,500));
		
	anim_events.push_back(explosionMush);
	anim_events.push_back(explosionCol);
	anim_events.push_back(shockwave);
	anim_events.push_back(baseExplosion);
	
	_started = false;

	lightNameExplosion = 0;
}

AnimationSequence::~AnimationSequence(void) {}

void AnimationSequence::prepareAnimationSequence(Ogre::Viewport* vp) {
	this->_vp = vp;
	for(int i = 0; i < anim_events.size(); i++){
		anim_events[i]->prepareEvent();
	}
	for(int i = 0; i < doubleShockWaves.size(); i++){
		doubleShockWaves[i]->prepareEvent();
	}
}

void AnimationSequence::restartAnimationSequence() {
	for(int i = 0; i < anim_events.size(); i++){
		anim_events[i]->restartEvent();
	}
		for(int i = 0; i < doubleShockWaves.size(); i++){
		doubleShockWaves[i]->restartEvent();
	}
	elapsedTime = 0;
}

void AnimationSequence::updateAnimationSequence(Ogre::Real time) {
	elapsedTime += time;
	if(elapsedTime > SHOCKWAVE_START_TIME && !shockwave->hasStarted()) {
		shockwave->prepareEvent();
		shockwave->startEvent();
	}
	else if(elapsedTime > FIRE_COLUMN_START_TIME && !explosionCol->hasStarted())  {
		explosionCol->prepareEvent();
		explosionCol->startEvent();
	}	
	else if(elapsedTime > FIRE_MUSH_START_TIME && !explosionMush->hasStarted()) {
		explosionMush->startEvent();
		for(int i = 0; i < doubleShockWaves.size(); i++){
			doubleShockWaves[i]->prepareEvent();
			doubleShockWaves[i]->startEvent();
		}
	}
	else if(elapsedTime > HEAT_EFFECT_FINISH) {
		Ogre::CompositorManager::getSingleton().removeCompositor(_vp, "Heat"); 
	}
	else if(elapsedTime > HEAT_EFFECT_START) {
		Ogre::CompositorManager::getSingleton().addCompositor(_vp, "Heat");
		Ogre::CompositorManager::getSingleton().setCompositorEnabled(_vp, "Heat", true);
	}

	//Disable efeito HDR
	if(elapsedTime > HDR_EFFECT_FINISH) {
		Ogre::ParticleSystem* hdr = _mScene->getParticleSystem("firstlight"+ Ogre::StringConverter::toString(lightNameExplosion-1));
		hdr->removeAllEmitters();
	}

	//Animacao no cogumelo
	explosionMush->update(elapsedTime);
}

void AnimationSequence::startAnimationSequence() {
		
	_started = true;

	startLightExplosion();

	baseExplosion->prepareEvent();
	baseExplosion->startEvent();
}

void AnimationSequence::startLightExplosion() {
	
	Ogre::ParticleSystem* particleSystem = _mScene->createParticleSystem("firstlight" + Ogre::StringConverter::toString(lightNameExplosion), "LightExplosion");
	lightNameExplosion++;
	Ogre::SceneNode* explosion = _mScene->getRootSceneNode()->createChildSceneNode();
	explosion->translate(5100,400,-10,Ogre::Node::TS_PARENT);
	explosion->attachObject(particleSystem);
	particleSystem->setRenderQueueGroup(Ogre::RENDER_QUEUE_9);
	
}


void AnimationSequence::quitAnimationSequence() {
	for(int i = 0; i < anim_events.size(); i++){
		anim_events[i]->quit();
	}
	for(int i = 0; i < doubleShockWaves.size(); i++){
		doubleShockWaves[i]->quit();
	}
	setStarted(false);
}

void AnimationSequence::setStarted(bool state) {
	_started = state;
	elapsedTime = 0;
}